﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace Xceed.Silverlight.Data
{
  internal class SyncParentEnumerator<T> : SyncEnumerator<T>
  {
    protected SyncParentEnumerator( ISyncEnumerator childEnumerator )
    {
      this.ChildEnumerator = childEnumerator;
    }

    #region ChildEnumerator Property

    internal ISyncEnumerator ChildEnumerator
    {
      get
      {
        return m_childEnumerator;
      }
      set
      {
        if( m_childEnumerator != value )
        {
          if( m_childEnumerator != null )
          {
            m_childEnumerator.DataChanged -= new EventHandler<DataChangedEventArgs>( ChildDataChanged );
          }

          m_childEnumerator = value;

          if( m_childEnumerator != null )
          {
            m_childEnumerator.DataChanged += new EventHandler<DataChangedEventArgs>( ChildDataChanged );
          }
        }
      }
    }

    #endregion ChildEnumerator Property

    #region ChildEnumeratorTyped Property

    internal ISyncEnumerator<T> ChildEnumeratorTyped
    {
      get
      {
        return ( ISyncEnumerator<T> )m_childEnumerator;
      }
    }

    #endregion ChildEnumeratorTyped Property

    protected virtual void ChildDataChanged( object sender, DataChangedEventArgs args )
    {
      this.RaiseDataChanged( args );
    }

    #region IDisposable Members

    private bool m_disposed;//false

    protected override void Dispose( bool disposing )
    {
      if( !m_disposed )
      {
        try
        {
          if( disposing )
          {
            if( m_childEnumerator != null )
            {
              m_childEnumerator.DataChanged -= ChildDataChanged;
            }

            if( m_childEnumerator is IDisposable )
            {
              ( ( IDisposable )m_childEnumerator ).Dispose();
            }
          }
          m_disposed = true;
        }
        finally
        {
          base.Dispose( disposing );
        }
      }
    }

    #endregion IDisposable Members

    #region ISyncEnumerator<T> Members

    public override bool JumpMoveAndFetch(
      JumpMoveAndFetchParameters parameters,
      out Nullable<long> actualMoveCount,
      out ICollection<T> fetchedItems,
      out SourcePosition endPosition,
      out RelativeOffset endPositionOffset,
      out EstimateInt64 totalCount )
    {
      return this.ChildEnumeratorTyped.JumpMoveAndFetch(
               parameters,
               out actualMoveCount,
               out fetchedItems,
               out endPosition,
               out endPositionOffset,
               out totalCount );
    }

    #endregion ISyncEnumerator<T> Members

    #region ISyncEnumerator Members

    public override GroupCountResult GroupCount( CountParameters parameters )
    {
      return this.ChildEnumerator.GroupCount( parameters );
    }

    public override CountResult Count( CountParameters parameters )
    {
      return this.ChildEnumerator.Count( parameters );
    }

    public override StatResults GetStatValues( StatParameters parameters )
    {
      return this.ChildEnumerator.GetStatValues( parameters );
    }

    public override bool JumpMoveAndFetch(
      JumpMoveAndFetchParameters parameters,
      out Nullable<long> actualMoveCount,
      out ICollection fetchedItems,
      out SourcePosition endPosition,
      out RelativeOffset endPositionOffset,
      out EstimateInt64 totalCount )
    {
      return this.ChildEnumeratorTyped.JumpMoveAndFetch(
               parameters,
               out actualMoveCount,
               out fetchedItems,
               out endPosition,
               out endPositionOffset,
               out totalCount );
    }

    #endregion ISyncEnumerator Members

    #region Private Fields

    private ISyncEnumerator m_childEnumerator; //null

    #endregion Private Fields
  }
}
